package com.isias.system.Controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.isias.common.result.Result;
import com.isias.common.result.ResultCodeEnum;
import com.isias.common.util.JwtHelper;
import com.isias.common.util.MD5;
import com.isias.model.system.SysUser;
import com.isias.model.vo.Captcha;
import com.isias.model.vo.LoginVo;
import com.isias.system.Service.IAuthenticationFacade;
import com.isias.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    private IAuthenticationFacade authenticationFacade; //获取当前用户对象类
    /**
     * 获取用户信息
     * */
    @GetMapping("/info")
    public Result Info(HttpServletRequest request){
        //获取请求头token字符串
        String token = request.getHeader("token");
        //从token中获取用户名称(id)
        String username = JwtHelper.getUsername(token);
        //根据用户名获取用户信息（基本信息、菜单权限以及按钮权限数据)
        Map<String, Object> map = sysUserService.getUserInfo(username);
        return Result.ok(map);
    }

    /**
     * 登录
     * 返回token
     * */
    @PostMapping("/login")
    public Result Login(@RequestBody LoginVo loginVo){
        Map<String, Object> map = sysUserService.Login(loginVo);
        return Result.ok(map);
    }

    /**
     * 注册接口
     * @param sysUser
     * @return
     */
    @PostMapping("register")
    public Result Register(@RequestBody SysUser sysUser){
        if(sysUserService.register(sysUser)){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 发送验证码
     * @param captcha
     * @return
     */
    @PostMapping ("getCaptcha")
    public Result GetCode(@RequestBody Captcha captcha){
        sysUserService.GetCode(captcha.getEmail());
        return Result.build(null,223,"验证码已发送，请按时接受");
    }

    /**
     * 判断验证码是否正确
     * @param code,email
     * @return
     */
    @GetMapping("CodeIsRight/{email}/{code}")
    public Result CodeIsRight(@PathVariable String email,@PathVariable String code){
        if (sysUserService.CodeIsRight(email,code)){
            return Result.build(true,223,"验证码正确");
        }
        return Result.fail();
    }

    /**
     * 更改密码
     * @param Password
     * @return
     */
    @PutMapping("UpdatePassword/{Password}")
    public Result UpdatePassword(@PathVariable String Password){
        Authentication authentication = authenticationFacade.getAuthentication();
        String username = authentication.getName();
        if (sysUserService.UpdatePassword(username,Password)){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 判断密码是否正确
     * @param Password
     * @return
     */
    @GetMapping("PasswordIsRight/{Password}")
    public Result PasswordIsRight(@PathVariable String Password){
        Authentication authentication = authenticationFacade.getAuthentication();
        String username = authentication.getName();
        if (sysUserService.PasswordIsRight(username,Password)){
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 登出
     * */
    @PostMapping("logout")
    public Result Logout(){
        Map<String, Object> map = new HashMap<>();
        map.put("data","success");
        return Result.ok(map);
    }


}
